'''
@author: haiwen
@date: 2020/9/23
@file: OrganizAPI.py
'''
#公司部分API

#1.删除所有部门--先列出所有部门，再根据返回的ID逐一删除
import requests

from conf.env import host, parent, spaceid


def delALL_organizations(cookies):
    orgs=list_organizations(cookies)['value'][1:]#去掉总公司的ID-去掉第一个
    for org in orgs:
        delete_organizations(org['_id'],cookies)

def delete_organizations(organization_id,cookies):
    resp=requests.delete(f'{host}/api/v4/organizations/{organization_id}',cookies=cookies)
    return resp.json()

#2.添加部门
def add_organizations(name,cookies):
    payload={ "name": name,
              "parent": parent,
              "sort_no": 100,
              "hidden": False,
              "space": spaceid
    }

    resp=requests.post(f'{host}/api/v4/organizations',json=payload,cookies=cookies)
    return resp.json()


#3.列出部门
def list_organizations(cookies):
    resp=requests.get(f'{host}/api/v4/organizations',cookies=cookies)
    return resp.json()

#4.修改部门
def edit_organizations(_id,name,cookies):
    payload={
        "$set": {
            "name": name,
            "parent": parent,
            "sort_no": 100,
            "hidden": False,
            "space": spaceid
        }
    }
    resp=requests.put(f'{host}/api/v4/organizations/{_id}',json=payload,cookies=cookies)
    return resp.json()